package com.sl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sl.mapper.DocumentMapper;
import com.sl.pojo.Document;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class DocumentService {
    @Resource
    private DocumentMapper documentMapper;

    public Document findByDocId(String docId){
        Example example = new Example(Document.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("docId",docId);
        Document document = documentMapper.selectOneByExample(example);
        return document;
    }


    //查：所有
    public List<Document> findAll(){
        System.out.println("查看所有文件");
        List<Document> documents = documentMapper.selectAll();
        return documents;
    }
    //查：主键
    public Document findById(Integer id){
        return documentMapper.selectByPrimaryKey(id);
    }
    //查：多条件查
   public Page  findList(Map<String,String> searchMap,Integer page,Integer pageSize){
       PageHelper.startPage(page,pageSize);
       Example example = createExample(searchMap);
       List<Document> documents = documentMapper.selectByExample(example);
       return  (Page<Document>)documents;
   }

    //加：一个
    public void addDocument(Document document){
        documentMapper.insert(document);
    }
    //改：一个
    public void update(Document document){
        documentMapper.updateByPrimaryKey(document);
    }

    /**删:逻辑---->把deleted的0改为1
     */
    public void delete(Document document) {
        document.setDeleted(1);
        documentMapper.updateByPrimaryKey(document);
    }
    /**
     * 删:真实
     */
    public void deleteReal(Integer id) {
        documentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 构建查询对象
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, String> searchMap){
        Example example=new Example(Document.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 主键id
            if(searchMap.get("id")!=null ){
                criteria.andEqualTo("id",searchMap.get("id"));
            }
            //文件唯一识别码。自己设定
            if(searchMap.get("docId")!=null ){
                criteria.andEqualTo("docId",searchMap.get("docId"));
            }
            // 是否删除
            if(searchMap.get("deleted")!=null ){
                criteria.andEqualTo("deleted",searchMap.get("deleted"));
            }
            // 文件类型
            if(searchMap.get("docType")!=null && !"".equals(searchMap.get("docType"))){
                criteria.andLike("docType","%"+searchMap.get("docType")+"%");
            }
            //文件地址
            if(searchMap.get("storageUrl")!=null && !"".equals(searchMap.get("storageUrl"))){
                criteria.andLike("storageUrl","%"+searchMap.get("storageUrl")+"%");
            }
            //文件名
            if(searchMap.get("docName")!=null && !"".equals(searchMap.get("docName"))){
                criteria.andLike("docName","%"+searchMap.get("docName")+"%");
            }
            //文件的描述
            if(searchMap.get("docDesc")!=null && !"".equals(searchMap.get("docDesc"))){
                criteria.andLike("docDesc","%"+searchMap.get("docDesc")+"%");
            }
            //公开类型：公开0，个人1
            if(searchMap.get("storageType")!=null ){
                criteria.andEqualTo("storageType",searchMap.get("storageType"));
            }
            //上传者名字
            if(searchMap.get("uploader")!=null && !"".equals(searchMap.get("uploader"))){
                criteria.andLike("uploader","%"+searchMap.get("uploader")+"%");
            }

        }
        return example;
    }

}
